#include "pg_common.h"
#include "create_shadow.h"

int main(int argc, char **argv){

  CONNECTION_DB conn = init_db(DB_ARQ_CONF);
  PGresult * result = get_shadow_table(conn);
  int i;
  char *outfile;

  if(argc == 2)
     outfile = strdup(argv[1]);
  else
     outfile = strdup(SHADOW_ARQ);

  FILE * f = fopen(outfile, "w");
  setspent();
  lckpwdf();
  for (i=0; i<PQntuples(result); i++){
   /* int j;*/
    struct spwd sp;
    sp.sp_namp = (char *)PQgetvalue(result,i,0);
    sp.sp_pwdp = "*";
    sp.sp_lstchg = (long int)atoi(PQgetvalue(result,i,2));
    sp.sp_min=(long int)atoi(PQgetvalue(result,i,3));
    sp.sp_max=(long int)atoi(PQgetvalue(result,i,4));
    sp.sp_warn=(long int)atoi(PQgetvalue(result,i,5));
    sp.sp_inact=(long int)atoi(PQgetvalue(result,i,6));
    sp.sp_expire=(long int)atoi(PQgetvalue(result,i,7));
    sp.sp_flag=0;
    /*int i = */putspent(&sp,f);
    
  }
  ulckpwdf();
  endspent();
  fclose(f);
  free_db(conn);
  return 0;

}

PGresult * get_shadow_table(CONNECTION_DB conn){
  PGresult * result = NULL;
  result = PQexec(conn->pgconn, shadow_select);
  return result;
}
